package io.bdmc.modules.bss.service.impl;

import io.bdmc.common.annotation.DataFilter;
import io.bdmc.common.utils.MyPage;
import io.bdmc.modules.bss.entity.DeviceAlarm;
import io.bdmc.modules.bss.entity.DeviceAlarmRealtime;
import io.bdmc.modules.bss.entity.DeviceFibreMatch;
import io.bdmc.modules.bss.mapper.DeviceAlarmMapper;
import io.bdmc.modules.bss.model.DeviceAlarmParam;
import io.bdmc.modules.bss.service.IDeviceAlarmService;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 设备报警 服务实现类
 * </p>
 *
 * @author ping
 * @since 2020-07-01
 */
@Service
public class DeviceAlarmServiceImpl extends ServiceImpl<DeviceAlarmMapper, DeviceAlarm> implements IDeviceAlarmService {
    @Autowired
    DeviceAlarmMapper _devicealarmMapper;

    @DataFilter(tableAlias = "d")
    @Override
    public MyPage<DeviceAlarm> mySelectPage(MyPage<DeviceAlarm> myPage, DeviceAlarmParam params) {
        return _devicealarmMapper.mySelectPage(myPage, params);
    }

    @Override
    public MyPage<DeviceAlarm> mySelectPagePermitAll(MyPage<DeviceAlarm> myPage, DeviceAlarmParam params) {
        return _devicealarmMapper.mySelectPage(myPage, params);
    }

    @Override
    public int alarmFibreCount(Long deptId) {
        return _devicealarmMapper.alarmFibreCount(deptId);
    }

    @Override
    public List<DeviceAlarm> selectAllLastAlarm() {
        return _devicealarmMapper.selectAllLastAlarm();
    }

    @Override
    public List<DeviceFibreMatch> deviceLastLengthAlarm() {
        return _devicealarmMapper.deviceLastLengthAlarm();
    }

    @Override
    public List<HashMap<String, Object>> alarmTypeSum(Long deptId) {
        return _devicealarmMapper.alarmTypeSum(deptId);
    }

    @Override
    public List<HashMap<String, Object>> alarmCodeSum(Long deptId) {
        return _devicealarmMapper.alarmCodeSum(deptId);
    }

    @Override
    public boolean saveByRealtimeAlarm(DeviceAlarmRealtime dbAlarm) {
        LocalDateTime now = LocalDateTime.now();
        String jsonString = JSONObject.toJSONString(dbAlarm);
        DeviceAlarm hisAlarm = JSONObject.parseObject(jsonString, DeviceAlarm.class);
        hisAlarm.setEndTime(now);
        hisAlarm.setAlarmId(0);
        hisAlarm.setProcessed(true);
        hisAlarm.setProcessBy("系统恢复正常");
        hisAlarm.setProcessTime(now);
        boolean result = save(hisAlarm);
        return result;
    }

    @Override
    public List<DeviceAlarmRealtime> selectHistoryAlarmList(DeviceAlarmParam param) {
        return _devicealarmMapper.selectHistoryAlarmList(param);
    }
}
